package cn.jit.util;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

public class SqlUtil {
	
	public static String toAddSql(Object obj) {
		if (obj == null) {
			return "";
		}
		
		Class<?> cls = obj.getClass();
		String simpleClassName = cls.getSimpleName();
		StringBuffer sBuffer = new StringBuffer("insert into ");
		sBuffer.append(simpleClassName).append(" ( ");
		
		List<String> mList = new ArrayList<String>();
		
		List vList = new ArrayList();
		
		Field[] fields = cls.getDeclaredFields();
		for (Field field : fields) {
			String mName = field.getName();
			System.out.println("字段名称=> " + mName);
			mList.add(mName);
			field.setAccessible(true);
			String name = field.getType().getSimpleName();
			System.out.println("字段类型=> " + name);
			try {
				Object value = field.get(obj);
				if (value instanceof String) {
					vList.add("\"" + value + "\"");
				} else {
					vList.add(value);
				}
				
				System.out.println("字段值=> " + value);
			} catch (Exception e) {
				e.printStackTrace();
			}
					
		}
		
		for (int i = 0; i < mList.size(); i++) {
            if (i < mList.size() - 1) {
                sBuffer.append(mList.get(i)).append(",");
            } else {
            	sBuffer.append(mList.get(i)).append(") values(");
            }
        }
        for (int i = 0; i < vList.size(); i++) {
            if (i < vList.size() - 1) {
            	sBuffer.append(vList.get(i)).append(",");
            } else {
            	sBuffer.append(vList.get(i)).append(")");
            }
        }
		
		return sBuffer.toString();
	}
	
	public static String addSql(Object obj) {
		if (obj == null) {
			return "";
		}
		
		Class<?> cls = obj.getClass();
		
		String className = cls.getName();
		String simpleClassName = cls.getSimpleName();
		
		StringBuffer sBuffer = new StringBuffer("insert into ");
		sBuffer.append(simpleClassName).append(" ( ");
		
		List<String> mList = new ArrayList<String>();
		List vList = new ArrayList();
		Method[] methods = cls.getMethods();
		for (Method item : methods) {
			String mName = item.getName();
			System.out.println(item.getName());
			if (mName.startsWith("get") && !mName.startsWith("getClass")) {
				String fieldName = mName.substring(3, mName.length());
				mList.add(fieldName);
				System.out.println("字段名字=> " + fieldName);
				try {
                    Object value = item.invoke(obj, null);
                    System.out.println("执行方法返回的值：" + value);
                    if (value instanceof String) {
                        vList.add("\"" + value + "\"");
                        System.out.println("字段值------>" + value);
                    } else {
                        vList.add(value);
                        System.out.println("字段值------>" + value);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
			}
		}
		
		for (int i = 0; i < mList.size(); i++) {
            if (i < mList.size() - 1) {
                sBuffer.append(mList.get(i)).append(",");
            } else {
            	sBuffer.append(mList.get(i)).append(") values(");
            }
        }
        for (int i = 0; i < vList.size(); i++) {
            if (i < vList.size() - 1) {
            	sBuffer.append(vList.get(i)).append(",");
            } else {
            	sBuffer.append(vList.get(i)).append(")");
            }
        }
		
		return sBuffer.toString();
	}
	
	public static String delSql(Object obj) {
		StringBuffer sBuffer = new StringBuffer();
		
		return sBuffer.toString();
	}
	
	public static String updSql(Object obj) {
		StringBuffer sBuffer = new StringBuffer();
		
		return sBuffer.toString();
	}
	
	public static String getSql(Object obj) {
		StringBuffer sBuffer = new StringBuffer();
		
		return sBuffer.toString();
	}
	
	
}
